﻿using BySoft.Server.Common.Enum;
using BySoft.Web.Utility;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BySoft.Web.Site.ComPage
{
    public partial class QueryFileProduct : BasePage
    {
        protected DataTable PSpDetail
        {
            get
            {
                if (Session["PSpData"] != null)
                {
                    return (DataTable)Session["PSpData"];
                }
                else
                {
                    return CreatePSp();
                }
            }
            set
            {
                Session["PSpData"] = value;
            }
        }

        private DataTable CreatePSp()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("ProductSpecialID");
            dt.Columns.Add("ProductID");
            dt.Columns.Add("SpecialCode");
            dt.Columns.Add("Model");
            dt.Columns.Add("Price");
            dt.Columns.Add("SpeWorkEvalDate");
            dt.Columns.Add("SpeVersionNum");
            dt.Columns.Add("FileNameSpe");
            dt.Columns.Add("FileUrlSpe");
            return dt;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                hidFileFormType.Value = UIHelper.GetQueryStringValue("id");
            }
        }

        private void SaveFile(string strDir)
        {
            string strName = DateTime.Now.ToString("yyyyMMddHHmmss");
            string strServerDir = Server.MapPath(strDir);
            if (!Directory.Exists(strServerDir))
            {
                Directory.CreateDirectory(strServerDir);
            }

            if (!string.IsNullOrEmpty(fimport.FileName))
            {
                string strUpFileName = fimport.FileName.Trim();
                string strExtension = strUpFileName.Substring(strUpFileName.LastIndexOf("."));
                string strSaveFileName = strName + strExtension;
                string strSavePath = strServerDir + strSaveFileName;
                fimport.SaveAs(strSavePath);
                FileInfo finfo = new FileInfo(strSavePath);
                DataTable dttemp = PSpDetail.Copy();
                DataRow[] rows = dttemp.Select("ProductSpecialID='" + UIHelper.GetQueryStringValue("id")+"'");
                if (rows.Length > 0)
                {
                    rows[0]["FileNameSpe"] = strUpFileName;
                    rows[0]["FileUrlSpe"] = finfo.Name;
                }
                dttemp.AcceptChanges();
                PSpDetail = dttemp;
            }
        }

        protected void btnOk_Click(object sender, EventArgs e)
        {
            string strPath = "~\\UpFile\\Product\\Special\\";

            SaveFile(strPath);
            string sReturn = "success";
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>setCookie(dialogReturn_key,'" + sReturn + "',1);CloseTopDialog();</script>");
        }
    }
}